package com.yincb.factorial;

import org.junit.Test;

public class FactorialDemo {

    /**
     * 1*2*3*4*5*6
     * f(n) = f(n-1)*n
     * @param n
     */
    public int recursion(int n){
        if (n<=1){
            return 1;
        }
        return recursion(n-1)*n;
    }

    @Test
    public void testRecursion(){
        System.out.println(recursion(6));
    }

    public int tailRecursion(int n,int current){
        System.out.println(n+"\t"+current);
        if (n <= 1){
            return current;
        }
        return tailRecursion(n-1,n*current);
    }

    @Test
    public void testTailRecursion(){
        System.out.println(tailRecursion(6,1));
    }

}
